package com.cyou.qb.front.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cyou.qb.front.jmx.DebugContext;
import com.sohu.fzBranch.SohuPassport;

public class PassportUtils {
	
	/**
	 * 校验用户是否登录
	 * @param req
	 * @param res
	 * @return
	 */
	public static boolean isLogin(HttpServletRequest req,HttpServletResponse res){
		if(DebugContext.debug){
			return DebugContext.login;
		}
		SohuPassport passport = new SohuPassport(req, res);
		boolean flag = false;
		try{
			flag = passport.userAuth()&&passport.isActivated()&&passport.isCertificateEmail();
			return flag;
		}catch(Exception e){
			return flag;
		}
	}
	
	/**
	 * 获取u_center_id  如果登录了返回u_center_id		如果没有登录返回 -1
	 * @param req
	 * @param res
	 * @return
	 */
	public static int getUserId(HttpServletRequest req,HttpServletResponse res){
		if(DebugContext.debug){
			return DebugContext.debugUser;
		}
		SohuPassport passport = new SohuPassport(req, res);
		if(passport.userAuth()&&passport.isActivated()&&passport.isCertificateEmail()){
			return Integer.parseInt(passport.getUserInfo("user_id"));
		}
		return -1;
	}
	
	public static String getUserIp(HttpServletRequest req,HttpServletResponse res){
		if(DebugContext.debug){
			return "0.0.0.0";
		}
		SohuPassport passport = new SohuPassport(req, res);
		return passport.getClientIp();
	}
}
